package com.cyufeng.blog.repository;

import com.cyufeng.blog.entity.Account;
import com.cyufeng.blog.entity.Article;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.List;

/**
 * 用户
 *
 * @author Cyufeng
 * Data:2018.12.30
 */
@Repository
public interface AccountRepository extends JpaRepository<Account, String>, JpaSpecificationExecutor<Account> {
    /**
     * 根据前台传进的数据查找数据库是否有对应的用户
     *
     * @param username
     * @param passwd
     * @return List<Login>
     */
    @Query(value = "select * from account t where t.DR ='0' and t.IS_ENABLE = '1' and t.USER_NAME = ?1 and t.PASSWD = ?2", nativeQuery = true)
    List<Account> getUser(String username, String passwd);

    /**
     * 得到所有的用户
     * 使用地点：后台管理界面的账户管理，
     *
     * @return
     */
//    @Query(value = "select * from account t where t.DR ='0' and t.IS_ENABLE = '1'",nativeQuery = true)
//    List<Account> getAllAccount();
    @Query(value = "select * from account t where t.DR ='0' and t.IS_ADMIN = ?", nativeQuery = true)
    List<Account> getAllAccount(int isAdmin);

    /**
     * 更新是否启用字段
     *
     * @return
     */
    @Modifying
    @Query(value = "update account t set IS_ENABLE = ? where t.DR ='0' and t.id =?", nativeQuery = true)
    void setIsEnable(int isEnable, int id);

    /**
     * 保存修改操作
     *
     * @return
     */
    @Modifying
    @Query(value = "update account t set PASSWD = ? where t.DR ='0' and t.id =?", nativeQuery = true)
    void saveEdit(String passwd, int id);

    /**
     * 删除用户
     *
     * @return
     */
    @Modifying
    @Query(value = "update account t set t.DR = 1,t.DEL_TIME = ? where t.id =?", nativeQuery = true)
    void deleteUser(Date date,int id);


}
